<template>
  <div class="page-wrapper">
    <!-- 页面头部，包含返回按钮和标题 -->
    <div class="header-wrapper">
      <h2 class="top">
        <van-icon class="back-icon" @click="fanhui" name="arrow-left" size="30px" />
        <span class="title-text">服务评价</span>
      </h2>
    </div>
    <!-- 加载状态显示 -->
    <div v-if="isLoading" class="loading">加载中...</div>
    <!-- 数据加载完成且存在时显示服务信息 -->
    <div v-else-if="servicesj && Object.keys(servicesj).length > 0">
      <div class="tuan-dui">
        <div class="tu-pian">
          <img src="https://tse1-mm.cn.bing.net/th/id/OIP-C.YhdOuoXmIUGUBSRSDLGXrwHaHa?rs=1&pid=ImgDetMain" alt="文章图片" />
        </div>
        <!-- 团队信息 -->
        <div class="tuan-dui-text">
          <span class="tuan-dui-text-title">{{ servicesj.doctorName }}</span>&emsp;
          <span class="jigo">{{ servicesj.position }}</span><br><br>
          <span class="jigo">所属机构：{{ servicesj.teamName }}</span><br><br>
        </div>
      </div>
      <!-- 提交按钮 -->
      <van-address-list
          add-button-text="提交"
          @add="open"
      />
    </div>
    <!-- 数据为空时显示提示信息 -->
    <div v-else class="no-data">暂无数据</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 服务包信息
      servicePackage: null,
      // 加载状态
      isLoading: false,
      // 医生姓名
      doctorName: '',
      // 服务申请详情数据
      servicesj: {}
    };
  },
  methods: {
    // 提交按钮点击事件，跳转到随访服务页面
    open() {
      this.$router.push('/followService');
    },
    // 根据记录 ID 获取服务申请详情
    findAll(recordId) {
      this.isLoading = true; // 开始加载数据
      this.axios.get(`/${recordId}`)
          .then(res => {
            console.log('请求响应:', res);
            if (res.data.code === 200) {
              this.servicesj = res.data.data;
              console.log('数据:', this.servicesj);
            } else {
              this.$message.error("获取失败");
            }
          })
          .catch(error => {
            console.error('请求出错:', error);
            this.$message.error('网络请求出错，请检查网络或服务器状态');
          })
          .finally(() => {
            this.isLoading = false; // 数据加载完成
          });
    },
    // 返回按钮点击事件，跳转到服务详情页面
    fanhui() {
      this.$router.push('/serviceDetail1');
    }
  },
  created() {
    const token = JSON.parse(localStorage.getItem("token"));
    if (!token) {
      this.$message.error('请先登录');
      setTimeout(() => {
        this.$router.push({ name: 'login' });
      }, 1000);
    } else {
      // 设置全局默认请求头
      this.axios.defaults.headers.common['token'] = token;

      // 从路由 query 参数中获取 doctorName 和 recordId
      const { doctorName, id: recordId } = this.$route.query;

      if (doctorName) {
        this.doctorName = doctorName;
      }

      // 调用 findAll 方法并传递 recordId
      if (recordId) {
        this.findAll(recordId);
      }
    }
  }
};
</script>

<style scoped>
.service-order-list {
  list-style: none;
  padding: 0;
}

.service-order-list li {
  margin-bottom: 20px;
  margin-left: 20px;
}

.label {
  display: inline-block;
  width: 100px;
  font-weight: bold;
}

.yuan {
  width: 100%;
  height: 80px;
  margin-top: 30px;
  text-align: center;
}

.page-wrapper {
  min-height: 100vh;
  background-color: #F5F9FD;
}

.header-wrapper {
  background-color: white;
}

.top {
  display: flex;
  align-items: center;
  height: 50px;
  position: relative;
}

.back-icon {
  position: absolute;
  left: 10px;
}

.title-text {
  width: 100%;
  text-align: center;
}

.article-text p {
  font-size: 12px;
  color: gray;
}

.tu-pian img {
  width: 150px;
  height: 150px;
  float: left;
  border-radius: 30px;
  box-shadow: 0 0 10px rgba(0, 128, 255, 0.2);
}

.tu-pian1 img {
  width: 80px;
  height: 80px;
  border-radius: 20px;
}

.ping-fen {
  margin-left: 10px;
  color: #2984F8;
  font-size: 18px;
}

.jigo {
  color: dimgrey;
}

.tuan-dui-text {
  width: 200px;
  height: 100px;
  float: left;
  margin-left: 10px;
  margin-top: 10px;
}

.tuan-dui-text-title {
  font-size: 20px;
  color: black;
}

.tu-pian {
  width: 150px;
  height: 150px;
  float: left;
  margin-left: 10px;
  margin-top: 10px;
}

.tuan-dui {
  width: 100%;
  height: 180px;
  background-color: white;
}

.tuan-dui1 {
  width: 100%;
  height: auto;
  background-color: white;
  padding: 10px 0px;
}

.loading {
  text-align: center;
  margin-top: 20px;
  color: #999;
}

.no-data {
  text-align: center;
  margin-top: 20px;
  color: #999;
}
</style>